package com.xjrsoft.module.ureport.service.impl;

import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.ureport.dto.ReportContentDto;
import com.xjrsoft.module.ureport.dto.ReportInfoDto;
import com.xjrsoft.module.ureport.entity.XjrReportContent;
import com.xjrsoft.module.ureport.entity.XjrReportInfo;
import com.xjrsoft.module.ureport.entity.XjrRptFile;
import com.xjrsoft.module.ureport.mapper.XjrReportContentMapper;
import com.xjrsoft.module.ureport.service.IXjrReportContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.ureport.service.IXjrReportInfoService;
import com.xjrsoft.module.ureport.vo.ReportContentOneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwq
 * @since 2021-04-14
 */
@Service
public class XjrReportContentServiceImpl extends ServiceImpl<XjrReportContentMapper, XjrReportContent> implements IXjrReportContentService {


    @Autowired
    private IXjrReportInfoService xjrReportInfoService;
    

    @Override
    public ReportContentOneVo getReportContentById(String id) throws Exception {
        XjrReportContent xjrReportContent = this.getById(id);
        if(xjrReportContent==null){
            throw new Exception("没有报表数据!!!");
        }
        return BeanUtil.copy(xjrReportContent, ReportContentOneVo.class);
    }

    @Override
    public boolean saveReportContent(ReportContentDto dto) {
        XjrReportContent xjrReportContent = BeanUtil.copy(dto, XjrReportContent.class);
        boolean saveFlag = baseMapper.insert(xjrReportContent) > 0;
        // 保存菜单
        XjrReportInfo reportInfo = BeanUtil.copy(dto, XjrReportInfo.class);
        reportInfo.set_file(xjrReportContent.getId());
        reportInfo.setCode("Report");
        reportInfo.setDeleteMark(0);
        reportInfo.setEnabledMark(1);
        boolean moduleSave = xjrReportInfoService.save(reportInfo);
        if (saveFlag && moduleSave) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateReportContent(String id, ReportContentDto dto) {
        XjrReportContent xjrReportContent = BeanUtil.copy(dto, XjrReportContent.class);
        xjrReportContent.setId(id);
        return baseMapper.updateById(xjrReportContent) > 0;
    }

    @Override
    public boolean deleteReportContent(String ids) throws Exception {
        List<String> idList = Arrays.asList(ids.split(StringPool.COMMA));
        if (CollectionUtil.isNotEmpty(idList)) {
            return this.removeByIds(idList);
        }else {
            throw new Exception("请选择要删除的信息!!");
        }
    }
}
